#include<iostream>
#include<fstream>
#include"contacts.pb.h"
using namespace std;

void PrintfPeopleInfo(contacts::Contacts& contacts)
{
    // 里面的内容相当于一个数组
    for(int i=0;i<contacts.contacts_size();i++)
    {
        cout<<"---------联系人"<<i+1<<"---------"<<endl;
        contacts::PeoleInfo people=contacts.contacts(i);
        cout<<"联系人名字："<<people.name()<<endl;
        cout<<"联系人年龄："<<people.age()<<endl;
        for(int j=0;j<people.phone_size();j++)
        {
            contacts::PeoleInfo_Phone phone=people.phone(j);
            cout<<"联系人电话"<<j+1<<":"<<phone.number()<<endl;
            cout<<"电话类型："<<j+1<<":"<<phone.PhoneType_Name(phone.type())<<endl;
        }
        if (people.has_data() && people.data().Is<contacts::Address>()) {
            contacts::Address address;
            people.data().UnpackTo(&address);
            if (!address.home_address().empty()) {
                cout << "联系人家庭地址：" << address.home_address() << endl;
            }
            if (!address.unit_address().empty()) {
                cout << "联系人单位地址：" << address.unit_address() << endl;
            }
        }
        switch(people.other_contact_case())
        {
            case contacts::PeoleInfo::OtherContactCase::kQq:
                cout<<"联系人的qq号为："<<people.qq()<<endl;
                break;
            case contacts::PeoleInfo::OtherContactCase::kWechat:
                cout<<"联系人微信号为："<<people.wechat()<<endl;
                break;
            default:
                cout<<"未设置其他联系方式"<<endl;
                break;
        }
        if (people.remark_size()) {
            cout << "备注信息：" << endl;
        }
        for(auto it=people.remark().cbegin;it<people.remark().cend();++it)
        {
            cout<<" "<<it->first<<":"<<it->second;
        }
    }
}
int main()
{
    //读取文件
    contacts::Contacts contacts;
    fstream input("contacts.bin",ios::in|ios::binary);
    if (!input)
    {
        std::cout << "文件不存在，创建了该文件" << std::endl;
    }
    else if (!contacts.ParseFromIstream(&input))
    {
        std::cout << "parse error!" << std::endl;
        input.close();
        return -1;
    }
    //打印通讯录
    PrintfPeopleInfo(contacts);
    return 0;
}